package cn.agiledata.bank.common.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @author jessik
 *
 * supply java sort util
 * 
 */
public class SortedListUtil {
	private static final Log logger = LogFactory.getLog(SortedListUtil.class);
	static class SortedListComparator implements Comparator{
		private final String propertyName;
		private final String sortOrder;
	    SortedListComparator(String propertyName,String sortOrder) {
	        this.propertyName = propertyName;
	        this.sortOrder = sortOrder;
	    }
	    public int compare(Object o1, Object o2) {
	    	Object co1=o1;
	    	Object co2=o2;
	    	if(sortOrder.equals("desc")){
	    		co1=o2;
		    	co2=o1;
	    	}
	        try {
	            Object pro1 = PropertyUtils.getProperty(co1, propertyName);
	            Object pro2 = PropertyUtils.getProperty(co2, propertyName);
	        	if(pro1==null){
	        		return 0;
	        	}
	        	if(pro2==null){
	        		return 1;
	        	}
	        	return ((Comparable)pro1).compareTo(
	            		pro2);
	        } catch (IllegalAccessException e) {
	            if (logger.isInfoEnabled()) {
	                logger.info("The object(" + co1 + ',' + co2
	                        + ") does not have a public getter for: "
	                        + propertyName);
	            }
	            return 0;
	        } catch (InvocationTargetException e) {
	            if (logger.isInfoEnabled()) {
	                logger.info("Error occured while invoke the getter of："
	                        + propertyName + "on object(" + co1 + ',' + co2 + ").", e
	                        .getTargetException());
	            }
	            return 0;
	        } catch (NoSuchMethodException e) {
	            if (logger.isInfoEnabled()) {
	                logger.info("The object(" + co1 + ',' + co2
	                        + ") does not have a getter for: " + propertyName);
	            }
	            return 0;
	        }catch (ClassCastException cce) {
	            if (logger.isInfoEnabled()) {
	                logger.info("The object(" + co1 + ',' + co2
	                        + ") does not have not comparable for: " + propertyName);
	            }
	            return 0;
	        }
	    }
	}
	public static List sort(List rawList,int pageNo, int recordPerPage,
			String sortColumn, String sortOrder){
		SortedListComparator comparator = new SortedListComparator(sortColumn,sortOrder);
		Collections.sort(rawList, comparator);
		int startIndex = (pageNo - 1) * recordPerPage;
        int endIndex = startIndex + recordPerPage;
        endIndex = (endIndex < rawList.size()) ? endIndex : rawList.size();
        return rawList.subList(startIndex, endIndex);
	}
}
